/* 变量 */
var luckdraw = "other/luckdraw.html";

// 大转盘
var contactsCallback_luckdraw = myApp.onPageInit('luckdraw', function (page) {

    // if (!hasAccount() || !hasOpenId()) {
    //     $$("#luckdraw_page .noluckdraw").show();
    //     return;
    // }

    //处理参数
    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    var openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量
    // var account = iss_localStorage.get("yuqing_appAccount") === ''?'b760689cb63a4a92834d6f14173bc293' : iss_localStorage.get("yuqing_appAccount"); // 全局变量
    // var openid = iss_localStorage.get("yuqing_appOpenId") === ''? 'oX816v4uhBMEXChYlScNbcq-uf0Y' : iss_localStorage.get("yuqing_appOpenId"); // 全局变量

    var zjl = false; //没中奖为false中奖为true
    var sort = 0; // 
    var recordGuid = ""; // 记录编号
    var prize = ""; // 奖项名称
    var errorMsg = "系统错误"; // 错误提示

    // 大转盘
    window.requestAnimFrame = (function () {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
                function (callback) {
                    window.setTimeout(callback, 1000 / 60)
                }
    })();
    var totalDeg = 360 * 3 + 0;
    var steps = [];

    var lostDeg = [360];
    var prizeDeg = "";
    var prize, sncode;
    var count = 0;
    var now = 0;
    var a = 0.01;
    var outter, inner, timer, running = false;
    function countSteps() {
        var t = Math.sqrt(2 * totalDeg / a);
        var v = a * t;
        for (var i = 0; i < t; i++) {
            steps.push((2 * v * i - a * i * i) / 2)
        }
        steps.push(totalDeg)
    }
    function step() {
        //alert('rotate(' + steps[now++] + 'deg)');
        outter.style.webkitTransform = 'rotate(' + steps[now++] + 'deg)';
        outter.style.MozTransform = 'rotate(' + steps[now++] + 'deg)';

        if (now < steps.length) {
            running = true;
            requestAnimFrame(step)
        } else {
            running = false;
            setTimeout(function () {
                // 完成处理
                SaveData();
            }, 200)
        }
    }

    function start(deg) {

        deg = deg || lostDeg[parseInt(lostDeg.length * Math.random())];
        running = true;
        clearInterval(timer);
        totalDeg = 360 * 5 + deg;
        steps = [];
        now = 0;
        countSteps();
        requestAnimFrame(step)
    }
    window.start = start;
    outter = document.getElementById('outer');
    inner = document.getElementById('inner');
    i = 10;
    $$("#inner").click(function () {

        CheckPopLogin(function () {
            account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
            openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量
            LoadData();
        });
        if (!hasUserIdNothing()) {
            return;
        }

        if (running) return;

        running = true;
        timer = setInterval(function () {
            i += 5;

            outter.style.webkitTransform = 'rotate(' + i + 'deg)';
            outter.style.MozTransform = 'rotate(' + i + 'deg)'
        }, 1);

        start(sort * 90);
        running = false;
        count++
    });

    // 初始化数据
    LoadData();

    // 加载数据
    function LoadData() {
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_luckdraw,
            data: { "action": "1", "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid + "'}" },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.success && data.datas) {
                    var datas = data.datas;

                    prize = datas.Name;
                    if (datas.IsAwards) {
                        zjl = true;
                        sort = data.Sort;
                    }
                    // 活动说明
                    var Content = "";
                    if (datas.Activity != null) {
                        Content = "<p class='red'>本次活动可以转" + datas.Activity.ActivityPersonMaxTimes + "次,当天可以转" + datas.Activity.ActivityDayMaxTime + "次,你已经转了<span id='times'>" + datas.Times + "</span>次!</p>"
                        $("#luckdraw_page #Content").html(Content);
                    }
                    // 奖品信息
                    var Awards = "";
                    if (datas.ActivityAwards != null) {
                        for (var i = 0; i < datas.ActivityAwards.length; i++) {
                            var p = "<p>" + datas.ActivityAwards[i].AwardsTitle + "： " + datas.ActivityAwards[i].AwardsName + "。奖品数量：" + datas.ActivityAwards[i].AwardsNum + " </p>";
                            Awards += p;
                        }
                        $("#luckdraw_page #Awards").html(Awards);
                    }
                    // 记录
                    if (datas.ActivityRecords && datas.ActivityRecords.length > 0
                        && datas.ActivityRecords[0] && datas.ActivityRecords[0] != null) {
                        recordGuid = datas.ActivityRecords[0].RecordGUID;
                    }
                    // 查询中奖纪录
                    SelectWinningRecord(datas.Activity.ActivityGUID);

                    errorMsg = data.msg;
                    $$("#luckdraw_page .luckdrawcontent").show();
                } else { /* alert("没有活动信息"); */$$("#luckdraw_page .noluckdraw").show(); }
            },
            error: function (xhr, status) {
                $$("#luckdraw_page .noluckdraw").show();
                console.log("luckdraw ajax error!");
            }
        }); // end ajax
    } // end loading

    // 保存数据
    function SaveData() {
        if (recordGuid == undefined || recordGuid == null || recordGuid == "") {
            myApp.alert(errorMsg);
            return;
        }
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_luckdraw,
            data: { "action": "2", "conditionParm": "{'RecordGuid':'" + recordGuid + "'}" },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.success) {
                    if (zjl) {
                        myApp.alert('恭喜您获得 ' + prize);
                    } else {
                        myApp.alert(prize);
                    }
                    LoadData();
                }
            },
            error: function (xhr, status) {
                console.log("ajax error!");
            }
        }); // end ajax
    }; // end loading

    // 查询中奖纪录
    function SelectWinningRecord(activityGuid) {
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_luckdraw,
            data: { "action": "7", "conditionParm": "{'ActivityGuid':'" + activityGuid + "'}" },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.success && data.datas) {
                    var datas = data.datas;
                    var ul = "";
                    if (datas.length > 0) {
                        for (var i = 0; i < datas.length; i++) {

                            var li = "<li class='item-content'>";
                            li += "<div class='item-inner'>";
                            li += "<div class='item-title'>" + datas[i].AwardsTitle + "</div>";
                            li += "<div class='item-after'>" + datas[i].NickName + "</div>";
                            li += " </div></li>";

                            ul += li;
                        }
                        $$('#luckdraw_page #ul_luckdraw').html(ul);
                    }
                }
            },
            error: function (xhr, status) {
                console.log("ajax error!");
            }
        }); // end ajax
    }; // end fun

});


function initData() {
    var href = window.location.href;
    if (href.indexOf(luckdraw) > -1) {
        contactsCallback_luckdraw.trigger();
    }
}

initData();